//
//  AddLiveView.m
//  MZSystem
//
//  Created by 智障 on 2018/11/8.
//  Copyright © 2018 zerod. All rights reserved.
//

#import "AddLiveView.h"

@interface AddLiveView()<UITextFieldDelegate,UITextViewDelegate>

@end
@implementation AddLiveView

- (void)setModel:(LiveDetailModel *)model{
    _model = model;
    
    if (model.cover.length > 0) {
        _addImage.hidden = YES;
        _addLabel.hidden = YES;
        [_bgImageButton sd_setImageWithURL:[NSURL URLWithString:model.cover] forState:UIControlStateNormal];
    }else{
        _addImage.hidden = NO;
        _addLabel.hidden = NO;
        [_bgImageButton setImage:nil forState:UIControlStateNormal];
    }
    
    if (model.title.length > 0) {
        _titleTF.text = model.title;
        _titleTipLabel.text = [NSString stringWithFormat:@"%lu/30",_titleTF.text.length];
    }
    
    if (model.beginTime.length > 0) {
        _beginTimeL.text = [self timeWithTimeEscString:model.beginTime];
    }
    if (model.endTime.length > 0) {
        _endTimeL.text = [self timeWithTimeEscString:model.endTime];
    }
    if (model.reporter.length > 0) {
        _reporterTF.text = model.reporter;
        _reporterTipL.text = [NSString stringWithFormat:@"%lu/30",_reporterTF.text.length];
    }
    [self reload_streamUserScrollerView];
    [self reload_reportersScrollerView];
    [self reload_directorsScrollerView];
    [self reload_editorsScrollerView];
    if (model.intro.length > 0) {
        _introTF.text = model.intro;
        _introTipL.text = [NSString stringWithFormat:@"%lu/500",_introTF.text.length];
        _intorPlaceLabel.hidden = model.intro.length == 0 ? NO : YES;
    }
    
}
- (void)reload_streamUserScrollerView{
    
    for (UIView *v in _streamUserScrollView.subviews) {
        [v removeFromSuperview];
    }
    if (self.model.streamUsers.length == 0) {
        return;
    }
    NSArray *streamArray = [self.model.streamUsers componentsSeparatedByString:@","];
//    NSArray *streamArray = @[@"aaa",@"bbb",@"ccc",@"ddd",@"eee",@"fff",@"qqq",];
    float witdh = 5;
    for (int i = 0; i < streamArray.count; i++){
        NSString * title = streamArray[i];
        
        float w = [self forWidthFromFont:FontSize14 withHeight:_streamUserScrollView.height withString:title] + 20 ;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(witdh,(_streamUserScrollView.height - 20)/2,w,20)];
        //            btn.tag = i + 101;
        //            [btn addTarget:self action:@selector(littleConditionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.backgroundColor = RGB(38,183,255);
        btn.titleLabel.font = FontSize14;
        btn.clipsToBounds = YES;
        btn.layer.cornerRadius = btn.height/2;
        btn.userInteractionEnabled = NO;
        witdh = w + witdh + 10;
        [_streamUserScrollView addSubview:btn];
    }
    _streamUserScrollView.contentSize = CGSizeMake(witdh,_streamUserScrollView.height);
}
- (void)reload_reportersScrollerView{
    for (UIView *v in _reportersReallyScrollView.subviews) {
        [v removeFromSuperview];
    }
    if (self.model.reporters.length == 0) {
        return;
    }
    NSArray *streamArray = [self.model.reporters componentsSeparatedByString:@","];
    //    NSArray *streamArray = @[@"aaa",@"bbb",@"ccc",@"ddd",@"eee",@"fff",@"qqq",];
    float witdh = 5;
    for (int i = 0; i < streamArray.count; i++){
        NSString * title = streamArray[i];
        
        float w = [self forWidthFromFont:FontSize14 withHeight:_reportersReallyScrollView.height withString:title] + 20 ;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(witdh,(_reportersReallyScrollView.height - 20)/2,w,20)];
        //            btn.tag = i + 101;
        //            [btn addTarget:self action:@selector(littleConditionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.backgroundColor = RGB(38,183,255);
        btn.titleLabel.font = FontSize14;
        btn.clipsToBounds = YES;
        btn.layer.cornerRadius = btn.height/2;
        btn.userInteractionEnabled = NO;
        witdh = w + witdh + 10;
        [_reportersReallyScrollView addSubview:btn];
    }
    _reportersReallyScrollView.contentSize = CGSizeMake(witdh,_reportersReallyScrollView.height);
}
- (void)reload_directorsScrollerView{
    for (UIView *v in _directorsScrollView.subviews) {
        [v removeFromSuperview];
    }
    if (self.model.directors.length == 0) {
        return;
    }
    NSArray *streamArray = [self.model.directors componentsSeparatedByString:@","];
    //    NSArray *streamArray = @[@"aaa",@"bbb",@"ccc",@"ddd",@"eee",@"fff",@"qqq",];
    float witdh = 5;
    for (int i = 0; i < streamArray.count; i++){
        NSString * title = streamArray[i];
        
        float w = [self forWidthFromFont:FontSize14 withHeight:_directorsScrollView.height withString:title] + 20 ;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(witdh,(_directorsScrollView.height - 20)/2,w,20)];
        //            btn.tag = i + 101;
        //            [btn addTarget:self action:@selector(littleConditionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.backgroundColor = RGB(38,183,255);
        btn.titleLabel.font = FontSize14;
        btn.clipsToBounds = YES;
        btn.layer.cornerRadius = btn.height/2;
        btn.userInteractionEnabled = NO;
        witdh = w + witdh + 10;
        [_directorsScrollView addSubview:btn];
    }
    _directorsScrollView.contentSize = CGSizeMake(witdh,_directorsScrollView.height);
}
- (void)reload_editorsScrollerView{
    for (UIView *v in _editorsScrollView.subviews) {
        [v removeFromSuperview];
    }
    if (self.model.editors.length == 0) {
        return;
    }
    NSArray *streamArray = [self.model.editors componentsSeparatedByString:@","];
    //    NSArray *streamArray = @[@"aaa",@"bbb",@"ccc",@"ddd",@"eee",@"fff",@"qqq",];
    float witdh = 5;
    for (int i = 0; i < streamArray.count; i++){
        NSString * title = streamArray[i];
        
        float w = [self forWidthFromFont:FontSize14 withHeight:_editorsScrollView.height withString:title] + 20 ;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(witdh,(_editorsScrollView.height - 20)/2,w,20)];
        //            btn.tag = i + 101;
        //            [btn addTarget:self action:@selector(littleConditionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.backgroundColor = RGB(38,183,255);
        btn.titleLabel.font = FontSize14;
        btn.clipsToBounds = YES;
        btn.layer.cornerRadius = btn.height/2;
        btn.userInteractionEnabled = NO;
        witdh = w + witdh + 10;
        [_editorsScrollView addSubview:btn];
    }
    _editorsScrollView.contentSize = CGSizeMake(witdh,_editorsScrollView.height);
}
- (IBAction)clickBgImageBtn:(UIButton *)sender {
    if (self.bgViewClick) {
        self.bgViewClick();
    }
}

-(void)awakeFromNib{
    [super awakeFromNib];
    _titleTF.delegate = self;
    [_titleTF addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    _reporterTF.delegate = self;
    [_reporterTF addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    
    [self.bgImageButton.imageView setContentMode:UIViewContentModeScaleAspectFill];
    self.bgImageButton.clipsToBounds = YES;
    
    _beginTimeL.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(timeBeginClick)];
    [_beginTimeL addGestureRecognizer:tapGesture];
    
    _endTimeL.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture_End = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(timeClick_End)];
    [_endTimeL addGestureRecognizer:tapGesture_End];
    
    _introTF.delegate = self;
    
    _streamUserScrollView.userInteractionEnabled = YES;
    UITapGestureRecognizer *streamUserScrollViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(streamUserScrollViewTapClick)];
    [_streamUserScrollView addGestureRecognizer:streamUserScrollViewTap];
    
    _reportersReallyScrollView.userInteractionEnabled = YES;
    UITapGestureRecognizer *reportersReallyScrollViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(reportersReallyScrollViewTapClick)];
    [_reportersReallyScrollView addGestureRecognizer:reportersReallyScrollViewTap];
    
    _directorsScrollView.userInteractionEnabled = YES;
    UITapGestureRecognizer *directorsScrollViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(directorsScrollViewTapClick)];
    [_directorsScrollView addGestureRecognizer:directorsScrollViewTap];
    
    _editorsScrollView.userInteractionEnabled = YES;
    UITapGestureRecognizer *editorsScrollViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(editorsScrollViewTapClick)];
    [_editorsScrollView addGestureRecognizer:editorsScrollViewTap];
    
}
- (void)streamUserScrollViewTapClick{
    if (self.scrollViewClick) {
        self.scrollViewClick(1);
    }
}
- (void)reportersReallyScrollViewTapClick{
    if (self.scrollViewClick) {
        self.scrollViewClick(2);
    }
}
- (void)directorsScrollViewTapClick{
    if (self.scrollViewClick) {
        self.scrollViewClick(3);
    }
}
- (void)editorsScrollViewTapClick{
    if (self.scrollViewClick) {
        self.scrollViewClick(4);
    }
}
-(void)timeBeginClick{
    [self.reporterTF resignFirstResponder];
    [self.titleTF resignFirstResponder];
    [self.introTF resignFirstResponder];
    if (self.timeClick) {
        self.timeClick(YES);
    }
    
}
-(void)timeClick_End{
    [self.reporterTF resignFirstResponder];
    [self.titleTF resignFirstResponder];
    [self.introTF resignFirstResponder];
    if (self.timeClick) {
        self.timeClick(NO);
    }
    
}
- (void)textFieldEditChanged:(UITextField *)textField{
    if (textField == _titleTF) {
        if (_titleTF.text.length > 30) {
            _titleTF.text = [_titleTF.text substringToIndex:30];
        }
        _titleTipLabel.text = [NSString stringWithFormat:@"%u/30",_titleTF.text.length];
    }
    if (textField == _reporterTF) {
        if (_reporterTF.text.length > 30) {
            _reporterTF.text = [_reporterTF.text substringToIndex:30];
        }
        _reporterTipL.text = [NSString stringWithFormat:@"%u/30",_reporterTF.text.length];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView{
    _introTipL.text = [NSString stringWithFormat:@"%u/500",_introTF.text.length];
    _intorPlaceLabel.hidden = textView.text.length == 0 ? NO : YES;
    if (_introTF.text.length > 500) {
        _introTF.text = [_introTF.text substringToIndex:500];
    }
}
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
- (NSString *)timeWithTimeEscString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"MM-dd HH:mm"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}
-(float)forWidthFromFont:(UIFont*)font  withHeight:(float)height withString:(NSString*)str
{
    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    style.lineBreakMode = NSLineBreakByWordWrapping;
    style.alignment = NSTextAlignmentLeft;
    CGSize size = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options: NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font, NSParagraphStyleAttributeName:style} context:nil].size;
    
    
    return size.width+1;
}
-(void)reloadData{
    [self reload_streamUserScrollerView];
    [self reload_reportersScrollerView];
    [self reload_directorsScrollerView];
    [self reload_editorsScrollerView];
}
@end
